home *** CD-ROM | disk | FTP | other *** search
/ Risc World 5 / Risc World 5.iso / SOFTWARE / Issue5 / PD / DIRSYNC / LegalStuff / gnudiff / util.c < prev    next >
C/C++ Source or Header  |  2004-12-19  |  19KB  |  781 lines

  1. /* Support routines for GNU DIFF.
  2.  
  3.    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002
  4.    Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU DIFF.
  7.  
  8.    GNU DIFF is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    GNU DIFF is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; see the file COPYING.
  20.    If not, write to the Free Software Foundation,
  21.    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  22.  
  23. #include "diff.h"
  24. #ifdef __riscos
  25. #include "basename.h"
  26. #else
  27. #include <dirname.h>
  28. #endif
  29. #include <error.h>
  30. #include <quotesys.h>
  31. #include <regex.h>
  32. #include <xalloc.h>
  33.  
  34. #ifdef __riscos
  35. #include <kernel.h>
  36. #endif
  37.  
  38. char const pr_program[] = PR_PROGRAM;
  39.  
  40. /* Queue up one-line messages to be printed at the end,
  41.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  42.  
  43. struct msg
  44. {
  45.   struct msg *next;
  46.   char args[1]; /* Format + 4 args, each '\0' terminated, concatenated.  */
  47. };
  48.  
  49. #ifndef __riscos
  50. /* Head of the chain of queues messages.  */
  51.  
  52. static struct msg *msg_chain;
  53.  
  54. /* Tail of the chain of queues messages.  */
  55.  
  56. static struct msg **msg_chain_end = &msg_chain;
  57.  
  58. /* Use when a system call returns non-zero status.
  59.    NAME should normally be the file name.  */
  60. #endif
  61.  
  62. void
  63. perror_with_name (char const *name)
  64. {
  65.   error (0, errno, "%s", name);
  66. }
  67.  
  68. /* Use when a system call returns non-zero status and that is fatal.  */
  69.  
  70. void
  71. pfatal_with_name (char const *name)
  72. {
  73.   int e = errno;
  74. #ifndef __riscos
  75.   print_message_queue ();
  76. #endif
  77.   error (EXIT_TROUBLE, e, "%s", name);
  78.   abort ();
  79. }
  80.  
  81. /* Print an error message containing MSGID, then exit.  */
  82.  
  83. void
  84. fatal (char const *msgid)
  85. {
  86. #ifndef __riscos
  87.   print_message_queue ();
  88. #endif
  89.   error (EXIT_TROUBLE, 0, "%s", _(msgid));
  90.   abort ();
  91. }
  92.  
  93. /* Like printf, except if -l in effect then save the message and print later.
  94.    This is used for things like "Only in ...".  */
  95.  
  96. void
  97. message (char const *format_msgid, char const *arg1, char const *arg2)
  98. {
  99.   message5 (format_msgid, arg1, arg2, 0, 0);
  100. }
  101.  
  102. void
  103. message5 (char const *format_msgid, char const *arg1, char const *arg2,
  104.       char const *arg3, char const *arg4)
  105. {
  106. #ifndef __riscos
  107.   if (paginate)
  108.     {
  109.       char *p;
  110.       char const *arg[5];
  111.       int i;
  112.       size_t size[5];
  113.       size_t total_size = offsetof (struct msg, args);
  114.       struct msg *new;
  115.  
  116.       arg[0] = format_msgid;
  117.       arg[1] = arg1;
  118.       arg[2] = arg2;
  119.       arg[3] = arg3 ? arg3 : "";
  120.       arg[4] = arg4 ? arg4 : "";
  121.  
  122.       for (i = 0;  i < 5;  i++)
  123.     total_size += size[i] = strlen (arg[i]) + 1;
  124.  
  125.       new = xmalloc (total_size);
  126.  
  127.       for (i = 0, p = new->args;  i < 5;  p += size[i++])
  128.     memcpy (p, arg[i], size[i]);
  129.  
  130.       *msg_chain_end = new;
  131.       new->next = 0;
  132.       msg_chain_end = &new->next;
  133.     }
  134.   else
  135. #endif
  136.     {
  137.       if (sdiff_merge_assist)
  138.     putchar (' ');
  139.       printf (_(format_msgid), arg1, arg2, arg3, arg4);
  140.     }
  141. }
  142.  
  143. #ifndef __riscos
  144. /* Output all the messages that were saved up by calls to `message'.  */
  145.  
  146. void
  147. print_message_queue (void)
  148. {
  149.   char const *arg[5];
  150.   int i;
  151.   struct msg *m = msg_chain;
  152.  
  153.   while (m)
  154.     {
  155.       struct msg *next = m->next;
  156.       arg[0] = m->args;
  157.       for (i = 0;  i < 4;  i++)
  158.     arg[i + 1] = arg[i] + strlen (arg[i]) + 1;
  159.       printf (_(arg[0]), arg[1], arg[2], arg[3], arg[4]);
  160.       free (m);
  161.       m = next;
  162.     }
  163. }
  164. #endif
  165.  
  166. /* Call before outputting the results of comparing files NAME0 and NAME1
  167.    to set up OUTFILE, the stdio stream for the output to go to.
  168.  
  169.    Usually, OUTFILE is just stdout.  But when -l was specified
  170.    we fork off a `pr' and make OUTFILE a pipe to it.
  171.    `pr' then outputs to our stdout.  */
  172.  
  173. static char const *current_name0;
  174. static char const *current_name1;
  175. static bool currently_recursive;
  176.  
  177. void
  178. setup_output (char const *name0, char const *name1, bool recursive)
  179. {
  180.   current_name0 = name0;
  181.   current_name1 = name1;
  182.   currently_recursive = recursive;
  183.   outfile = 0;
  184. }
  185.  
  186. #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
  187. static pid_t pr_pid;
  188. #endif
  189.  
  190. void
  191. begin_output (void)
  192. {
  193.   char *name;
  194.  
  195.   if (outfile != 0)
  196.     return;
  197.  
  198.   /* Construct the header of this piece of diff.  */
  199.   name = xmalloc (strlen (current_name0) + strlen (current_name1)
  200.           + strlen (switch_string) + 7);
  201.  
  202.   /* POSIX 1003.1-2001 specifies this format.  But there are some bugs in
  203.      the standard: it says that we must print only the last component
  204.      of the pathnames, and it requires two spaces after "diff" if
  205.      there are no options.  These requirements are silly and do not
  206.      match historical practice.  */
  207.   sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
  208.  
  209. #ifndef __riscos
  210.   if (paginate)
  211.     {
  212.       if (fflush (stdout) != 0)
  213.     pfatal_with_name (_("write failed"));
  214.  
  215.       /* Make OUTFILE a pipe to a subsidiary `pr'.  */
  216.       {
  217. #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
  218.     int pipes[2];
  219.  
  220.     if (pipe (pipes) != 0)
  221.       pfatal_with_name ("pipe");
  222.  
  223.     pr_pid = vfork ();
  224.     if (pr_pid < 0)
  225.       pfatal_with_name ("fork");
  226.  
  227.     if (pr_pid == 0)
  228.       {
  229.         close (pipes[1]);
  230.         if (pipes[0] != STDIN_FILENO)
  231.           {
  232.         if (dup2 (pipes[0], STDIN_FILENO) < 0)
  233.           pfatal_with_name ("dup2");
  234.         close (pipes[0]);
  235.           }
  236.  
  237.         execl (pr_program, pr_program, "-h", name, 0);
  238.         _exit (errno == ENOEXEC ? 126 : 127);
  239.       }
  240.     else
  241.       {
  242.         close (pipes[0]);
  243.         outfile = fdopen (pipes[1], "w");
  244.         if (!outfile)
  245.           pfatal_with_name ("fdopen");
  246.       }
  247. #else
  248.     char *command = xmalloc (sizeof pr_program - 1 + 7
  249.                  + quote_system_arg ((char *) 0, name) + 1);
  250.     char *p;
  251.     sprintf (command, "%s -f -h ", pr_program);
  252.     p = command + sizeof pr_program - 1 + 7;
  253.     p += quote_system_arg (p, name);
  254.     *p = 0;
  255.     errno = 0;
  256.     outfile = popen (command, "w");
  257.     if (!outfile)
  258.       pfatal_with_name (command);
  259.     free (command);
  260. #endif
  261.       }
  262.     }
  263.   else
  264. #endif /* !__riscos */
  265.     {
  266.  
  267.       /* If -l was not specified, output the diff straight to `stdout'.  */
  268.  
  269.       outfile = stdout;
  270.  
  271.       /* If handling multiple files (because scanning a directory),
  272.      print which files the following output is about.  */
  273.       if (currently_recursive)
  274.     printf ("%s\n", name);
  275.     }
  276.  
  277.   free (name);
  278.  
  279.   /* A special header is needed at the beginning of context output.  */
  280.   switch (output_style)
  281.     {
  282.     case OUTPUT_CONTEXT:
  283.       print_context_header (files, 0);
  284.       break;
  285.  
  286.     case OUTPUT_UNIFIED:
  287.       print_context_header (files, 1);
  288.       break;
  289.  
  290.     default:
  291.       break;
  292.     }
  293. }
  294.  
  295. /* Call after the end of output of diffs for one file.
  296.    Close OUTFILE and get rid of the `pr' subfork.  */
  297.  
  298. void
  299. finish_output (void)
  300. {
  301.   if (outfile != 0 && outfile != stdout)
  302.     {
  303.       int wstatus;
  304.       int werrno = 0;
  305.       if (ferror (outfile))
  306.     fatal ("write failed");
  307. #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK)
  308.       wstatus = pclose (outfile);
  309.       if (wstatus == -1)
  310.     werrno = errno;
  311. #else
  312.       if (fclose (outfile) != 0)
  313.     pfatal_with_name (_("write failed"));
  314.       if (waitpid (pr_pid, &wstatus, 0) < 0)
  315.     pfatal_with_name ("waitpid");
  316. #endif
  317.       if (! werrno && WIFEXITED (wstatus) && WEXITSTATUS (wstatus) == 127)
  318.     error (EXIT_TROUBLE, 0, _("subsidiary program `%s' not found"),
  319.            pr_program);
  320.       if (wstatus != 0)
  321.     error (EXIT_TROUBLE, werrno, _("subsidiary program `%s' failed"),
  322.            pr_program);
  323.     }
  324.  
  325.   outfile = 0;
  326. }
  327.  
  328. /* Compare two lines (typically one from each input file)
  329.    according to the command line options.
  330.    For efficiency, this is invoked only when the lines do not match exactly
  331.    but an option like -i might cause us to ignore the difference.
  332.    Return nonzero if the lines differ.  */
  333.  
  334. bool
  335. lines_differ (char const *s1, char const *s2)
  336. {
  337.   register unsigned char const *t1 = (unsigned char const *) s1;
  338.   register unsigned char const *t2 = (unsigned char const *) s2;
  339.   size_t column = 0;
  340.  
  341.   while (1)
  342.     {
  343.       register unsigned char c1 = *t1++;
  344.       register unsigned char c2 = *t2++;
  345.  
  346.       /* Test for exact char equality first, since it's a common case.  */
  347.       if (c1 != c2)
  348.     {
  349.       switch (ignore_white_space)
  350.         {
  351.         case IGNORE_ALL_SPACE:
  352.           /* For -w, just skip past any white space.  */
  353.           while (ISSPACE (c1) && c1 != '\n') c1 = *t1++;
  354.           while (ISSPACE (c2) && c2 != '\n') c2 = *t2++;
  355.           break;
  356.  
  357.         case IGNORE_SPACE_CHANGE:
  358.           /* For -b, advance past any sequence of white space in
  359.          line 1 and consider it just one space, or nothing at
  360.          all if it is at the end of the line.  */
  361.           if (ISSPACE (c1))
  362.         {
  363.           while (c1 != '\n')
  364.             {
  365.               c1 = *t1++;
  366.               if (! ISSPACE (c1))
  367.             {
  368.               --t1;
  369.               c1 = ' ';
  370.               break;
  371.             }
  372.             }
  373.         }
  374.  
  375.           /* Likewise for line 2.  */
  376.           if (ISSPACE (c2))
  377.         {
  378.           while (c2 != '\n')
  379.             {
  380.               c2 = *t2++;
  381.               if (! ISSPACE (c2))
  382.             {
  383.               --t2;
  384.               c2 = ' ';
  385.               break;
  386.             }
  387.             }
  388.         }
  389.  
  390.           if (c1 != c2)
  391.         {
  392.           /* If we went too far when doing the simple test
  393.              for equality, go back to the first non-white-space
  394.              character in both sides and try again.  */
  395.           if (c2 == ' ' && c1 != '\n'
  396.               && (unsigned char const *) s1 + 1 < t1
  397.               && ISSPACE (t1[-2]))
  398.             {
  399.               --t1;
  400.               continue;
  401.             }
  402.           if (c1 == ' ' && c2 != '\n'
  403.               && (unsigned char const *) s2 + 1 < t2
  404.               && ISSPACE (t2[-2]))
  405.             {
  406.               --t2;
  407.               continue;
  408.             }
  409.         }
  410.  
  411.           break;
  412.  
  413.         case IGNORE_TAB_EXPANSION:
  414.           if ((c1 == ' ' && c2 == '\t')
  415.           || (c1 == '\t' && c2 == ' '))
  416.         {
  417.           size_t column2 = column;
  418.           for (;; c1 = *t1++)
  419.             {
  420.               if (c1 == ' ')
  421.             column++;
  422.               else if (c1 == '\t')
  423.             column += TAB_WIDTH - column % TAB_WIDTH;
  424.               else
  425.             break;
  426.             }
  427.           for (;; c2 = *t2++)
  428.             {
  429.               if (c2 == ' ')
  430.             column2++;
  431.               else if (c2 == '\t')
  432.             column2 += TAB_WIDTH - column2 % TAB_WIDTH;
  433.               else
  434.             break;
  435.             }
  436.           if (column != column2)
  437.             return 1;
  438.         }
  439.           break;
  440.  
  441.         case IGNORE_NO_WHITE_SPACE:
  442.           break;
  443.         }
  444.  
  445.       /* Lowercase all letters if -i is specified.  */
  446.  
  447.       if (ignore_case)
  448.         {
  449.           c1 = TOLOWER (c1);
  450.           c2 = TOLOWER (c2);
  451.         }
  452.  
  453.       if (c1 != c2)
  454.         break;
  455.     }
  456.       if (c1 == '\n')
  457.     return 0;
  458.  
  459.       column += c1 == '\t' ? TAB_WIDTH - column % TAB_WIDTH : 1;
  460.     }
  461.  
  462.   return 1;
  463. }
  464.  
  465. /* Find the consecutive changes at the start of the script START.
  466.    Return the last link before the first gap.  */
  467.  
  468. struct change *
  469. find_change (struct change *start)
  470. {
  471.   return start;
  472. }
  473.  
  474. struct change *
  475. find_reverse_change (struct change *start)
  476. {
  477.   return start;
  478. }
  479.  
  480. /* Divide SCRIPT into pieces by calling HUNKFUN and
  481.    print each piece with PRINTFUN.
  482.    Both functions take one arg, an edit script.
  483.  
  484.    HUNKFUN is called with the tail of the script
  485.    and returns the last link that belongs together with the start
  486.    of the tail.
  487.  
  488.    PRINTFUN takes a subscript which belongs together (with a null
  489.    link at the end) and prints it.  */
  490.  
  491. void
  492. print_script (struct change *script,
  493.           struct change * (*hunkfun) (struct change *),
  494.           void (*printfun) (struct change *))
  495. {
  496.   struct change *next = script;
  497.  
  498.   while (next)
  499.     {
  500.       struct change *this, *end;
  501.  
  502.       /* Find a set of changes that belong together.  */
  503.       this = next;
  504.       end = (*hunkfun) (next);
  505.  
  506.       /* Disconnect them from the rest of the changes,
  507.      making them a hunk, and remember the rest for next iteration.  */
  508.       next = end->link;
  509.       end->link = 0;
  510. #ifdef DEBUG
  511.       debug_script (this);
  512. #endif
  513.  
  514.       /* Print this hunk.  */
  515.       (*printfun) (this);
  516.  
  517.       /* Reconnect the script so it will all be freed properly.  */
  518.       end->link = next;
  519.     }
  520. }
  521.  
  522. /* Print the text of a single line LINE,
  523.    flagging it with the characters in LINE_FLAG (which say whether
  524.    the line is inserted, deleted, changed, etc.).  */
  525.  
  526. void
  527. print_1_line (char const *line_flag, char const *const *line)
  528. {
  529.   char const *base = line[0], *limit = line[1]; /* Help the compiler.  */
  530.   FILE *out = outfile; /* Help the compiler some more.  */
  531.   char const *flag_format = 0;
  532.  
  533.   /* If -T was specified, use a Tab between the line-flag and the text.
  534.      Otherwise use a Space (as Unix diff does).
  535.      Print neither space nor tab if line-flags are empty.  */
  536.  
  537.   if (line_flag && *line_flag)
  538.     {
  539.       flag_format = initial_tab ? "%s\t" : "%s ";
  540.       fprintf (out, flag_format, line_flag);
  541.     }
  542.  
  543.   output_1_line (base, limit, flag_format, line_flag);
  544.  
  545.   if ((!line_flag || line_flag[0]) && limit[-1] != '\n')
  546.     fprintf (out, "\n\\ %s\n", _("No newline at end of file"));
  547. }
  548.  
  549. /* Output a line from BASE up to LIMIT.
  550.    With -t, expand white space characters to spaces, and if FLAG_FORMAT
  551.    is nonzero, output it with argument LINE_FLAG after every
  552.    internal carriage return, so that tab stops continue to line up.  */
  553.  
  554. void
  555. output_1_line (char const *base, char const *limit, char const *flag_format,
  556.            char const *line_flag)
  557. {
  558.   if (!expand_tabs)
  559.     fwrite (base, limit - base, 1, outfile);
  560.   else
  561.     {
  562.       register FILE *out = outfile;
  563.       register unsigned char c;
  564.       register char const *t = base;
  565.       register unsigned int column = 0;
  566.  
  567.       while (t < limit)
  568.     switch ((c = *t++))
  569.       {
  570.       case '\t':
  571.         {
  572.           unsigned int spaces = TAB_WIDTH - column % TAB_WIDTH;
  573.           column += spaces;
  574.           do
  575.         putc (' ', out);
  576.           while (--spaces);
  577.         }
  578.         break;
  579.  
  580.       case '\r':
  581.         putc (c, out);
  582.         if (flag_format && t < limit && *t != '\n')
  583.           fprintf (out, flag_format, line_flag);
  584.         column = 0;
  585.         break;
  586.  
  587.       case '\b':
  588.         if (column == 0)
  589.           continue;
  590.         column--;
  591.         putc (c, out);
  592.         break;
  593.  
  594.       default:
  595.         if (ISPRINT (c))
  596.           column++;
  597.         putc (c, out);
  598.         break;
  599.       }
  600.     }
  601. }
  602.  
  603. char const change_letter[] = { 0, 'd', 'a', 'c' };
  604.  
  605. /* Translate an internal line number (an index into diff's table of lines)
  606.    into an actual line number in the input file.
  607.    The internal line number is I.  FILE points to the data on the file.
  608.  
  609.    Internal line numbers count from 0 starting after the prefix.
  610.    Actual line numbers count from 1 within the entire file.  */
  611.  
  612. lin
  613. translate_line_number (struct file_data const *file, lin i)
  614. {
  615.   return i + file->prefix_lines + 1;
  616. }
  617.  
  618. /* Translate a line number range.  This is always done for printing,
  619.    so for convenience translate to long rather than lin, so that the
  620.    caller can use printf with "%ld" without casting.  */
  621.  
  622. void
  623. translate_range (struct file_data const *file,
  624.          lin a, lin b,
  625.          long *aptr, long *bptr)
  626. {
  627.   *aptr = translate_line_number (file, a - 1) + 1;
  628.   *bptr = translate_line_number (file, b + 1) - 1;
  629. }
  630.  
  631. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  632.    If the two numbers are identical, print just one number.
  633.  
  634.    Args A and B are internal line numbers.
  635.    We print the translated (real) line numbers.  */
  636.  
  637. void
  638. print_number_range (char sepchar, struct file_data *file, lin a, lin b)
  639. {
  640.   long trans_a, trans_b;
  641.   translate_range (file, a, b, &trans_a, &trans_b);
  642.  
  643.   /* Note: we can have B < A in the case of a range of no lines.
  644.      In this case, we should print the line number before the range,
  645.      which is B.  */
  646.   if (trans_b > trans_a)
  647.     fprintf (outfile, "%ld%c%ld", trans_a, sepchar, trans_b);
  648.   else
  649.     fprintf (outfile, "%ld", trans_b);
  650. }
  651.  
  652. /* Look at a hunk of edit script and report the range of lines in each file
  653.    that it applies to.  HUNK is the start of the hunk, which is a chain
  654.    of `struct change'.  The first and last line numbers of file 0 are stored in
  655.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
  656.    Note that these are internal line numbers that count from 0.
  657.  
  658.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  659.  
  660.    Return UNCHANGED if only ignorable lines are inserted or deleted,
  661.    OLD if lines of file 0 are deleted,
  662.    NEW if lines of file 1 are inserted,
  663.    and CHANGED if both kinds of changes are found. */
  664.  
  665. enum changes
  666. analyze_hunk (struct change *hunk,
  667.           lin *first0, lin *last0,
  668.           lin *first1, lin *last1)
  669. {
  670.   struct change *next;
  671.   lin l0, l1;
  672.   lin show_from, show_to;
  673.   lin i;
  674.   bool trivial = ignore_blank_lines || ignore_regexp.fastmap;
  675.   size_t trivial_length = (int) ignore_blank_lines - 1;
  676.     /* If 0, ignore zero-length lines;
  677.        if SIZE_MAX, do not ignore lines just because of their length.  */
  678.  
  679.   char const * const *linbuf0 = files[0].linbuf;  /* Help the compiler.  */
  680.   char const * const *linbuf1 = files[1].linbuf;
  681.  
  682.   show_from = show_to = 0;
  683.  
  684.   *first0 = hunk->line0;
  685.   *first1 = hunk->line1;
  686.  
  687.   next = hunk;
  688.   do
  689.     {
  690.       l0 = next->line0 + next->deleted - 1;
  691.       l1 = next->line1 + next->inserted - 1;
  692.       show_from += next->deleted;
  693.       show_to += next->inserted;
  694.  
  695.       for (i = next->line0; i <= l0 && trivial; i++)
  696.     {
  697.       char const *line = linbuf0[i];
  698.       size_t len = linbuf0[i + 1] - line - 1;
  699.       if (len != trivial_length
  700.           && (! ignore_regexp.fastmap
  701.           || re_search (&ignore_regexp, line, len, 0, len, 0) < 0))
  702.         trivial = 0;
  703.     }
  704.  
  705.       for (i = next->line1; i <= l1 && trivial; i++)
  706.     {
  707.       char const *line = linbuf1[i];
  708.       size_t len = linbuf1[i + 1] - line - 1;
  709.       if (len != trivial_length
  710.           && (! ignore_regexp.fastmap
  711.           || re_search (&ignore_regexp, line, len, 0, len, 0) < 0))
  712.         trivial = 0;
  713.     }
  714.     }
  715.   while ((next = next->link) != 0);
  716.  
  717.   *last0 = l0;
  718.   *last1 = l1;
  719.  
  720.   /* If all inserted or deleted lines are ignorable,
  721.      tell the caller to ignore this hunk.  */
  722.  
  723.   if (trivial)
  724.     return UNCHANGED;
  725.  
  726.   return (show_from ? OLD : UNCHANGED) | (show_to ? NEW : UNCHANGED);
  727. }
  728.  
  729. /* Concatenate three strings, returning a newly malloc'd string.  */
  730.  
  731. char *
  732. concat (char const *s1, char const *s2, char const *s3)
  733. {
  734.   char *new = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1);
  735.   sprintf (new, "%s%s%s", s1, s2, s3);
  736.   return new;
  737. }
  738.  
  739. /* Yield a new block of SIZE bytes, initialized to zero.  */
  740.  
  741. void *
  742. zalloc (size_t size)
  743. {
  744.   void *p = xmalloc (size);
  745.   memset (p, 0, size);
  746.   return p;
  747. }
  748.  
  749. /* Yield the newly malloc'd pathname
  750.    of the file in DIR whose filename is FILE.  */
  751.  
  752. char *
  753. dir_file_pathname (char const *dir, char const *file)
  754. {
  755.   char const *base = base_name (dir);
  756.   bool omit_slash = !*base || base[strlen (base) - 1] == '/';
  757. #ifdef __riscos
  758.   return concat (dir, "." + omit_slash, file);
  759. #else
  760.   return concat (dir, "/" + omit_slash, file);
  761. #endif
  762. }
  763.  
  764. void
  765. debug_script (struct change *sp)
  766. {
  767.   fflush (stdout);
  768.  
  769.   for (; sp; sp = sp->link)
  770.     {
  771.       long line0 = sp->line0;
  772.       long line1 = sp->line1;
  773.       long deleted = sp->deleted;
  774.       long inserted = sp->inserted;
  775.       fprintf (stderr, "%3ld %3ld delete %ld insert %ld\n",
  776.            line0, line1, deleted, inserted);
  777.     }
  778.  
  779.   fflush (stderr);
  780. }
  781.